package com.auto.api.common.entity;

import com.auto.api.common.enums.ResultCode;
import com.auto.api.common.enums.ResultEnum;
import java.io.Serializable;

/**
 * @program:
 * @description: 返回数据结果
 * @author: liangshf
 * @create: 2022-01-04
 */
public class ResultData<T> implements Serializable {
    private static final long serialVersionUID = -1493801726325639138L;

    private String code;
    private T data;
    private String message;

    private static ResultData result = new ResultData();

    static {
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getDesc());
    }

    public ResultData() {
        this(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc());
    }

    public ResultData(T data, String code, String message) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResultData(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResultData(T data) {
        this(data, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getDesc());
    }

    public ResultData(ResultEnum<String> result) {
        this.code = result.getCode();
        this.message = result.getDesc();
    }

    private ResultData(T data, ResultEnum<String> result) {
        this.code = result.getCode();
        this.message = result.getDesc();
        this.data = data;
    }

    public static ResultData ok() {
        return result;
    }

    public static <T> ResultData<T> ok(T data) {
        return new ResultData<>(data);
    }

    public static <T> ResultData<T> err() {
        return new ResultData(ResultCode.BUSINESS_ERROR.getCode(), ResultCode.BUSINESS_ERROR.getDesc());
    }

    public static <T> ResultData<T> err(String msg) {
        return new ResultData(ResultCode.BUSINESS_ERROR.getCode(), msg);
    }

    public static <T> ResultData<T> err(ResultEnum<String> result) {
        return new ResultData(result.getCode(), result.getDesc());
    }

    public static <T> ResultData<T> err(String code, String msg) {
        return new ResultData(code, msg);
    }

    public static <T> ResultData<T> err(T data) {
        return new ResultData(data, ResultCode.BUSINESS_ERROR.getCode(), ResultCode.BUSINESS_ERROR.getDesc());
    }

    public static <T> ResultData<T> err(T data, ResultEnum<String> result) {
        return new ResultData(data, result.getCode(), result.getDesc());
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}